package com.ruoyi.project.yuwen.yuwenfeedback.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.yuwen.yuwenfeedback.domain.AudioFeedback;
import com.ruoyi.project.yuwen.yuwenfeedback.service.IAudioFeedbackService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 朗读举报Controller
 *
 * @author shixiangwen
 * @date 2021-02-24
 */
@Controller
@RequestMapping("/yuwen/yuwenfeedback")
public class AudioFeedbackController extends BaseController {

    private String prefix = "yuwen/yuwenfeedback";

    @Autowired
    private IAudioFeedbackService audioFeedbackService;

    @RequiresPermissions("yuwen:yuwenfeedback:view")
    @GetMapping()
    public String yuwenfeedback() {
        return prefix + "/yuwenfeedback";
    }

    /**
     * 查询朗读举报列表
     */
    @RequiresPermissions("yuwen:yuwenfeedback:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AudioFeedback audioFeedback) {
        startPage();
        List<AudioFeedback> list = audioFeedbackService.selectAudioFeedbackList(audioFeedback);
        return getDataTable(list);
    }

    /**
     * 导出朗读举报列表
     */
    @RequiresPermissions("yuwen:yuwenfeedback:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AudioFeedback audioFeedback) {
        List<AudioFeedback> list = audioFeedbackService.selectAudioFeedbackList(audioFeedback);
        ExcelUtil<AudioFeedback> util = new ExcelUtil<AudioFeedback>(AudioFeedback.class);
        return util.exportExcel(list, "yuwenfeedback");
    }

    /**
     * 新增朗读举报
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存朗读举报
     */
    @RequiresPermissions("yuwen:yuwenfeedback:add")
    @Log(title = "朗读举报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AudioFeedback audioFeedback) {
        return toAjax(audioFeedbackService.insertAudioFeedback(audioFeedback));
    }

    /**
     * 修改朗读举报
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AudioFeedback audioFeedback = audioFeedbackService.selectAudioFeedbackById(id);
        mmap.put("audioFeedback", audioFeedback);
        return prefix + "/edit";
    }

    /**
     * 修改保存朗读举报
     */
    @RequiresPermissions("yuwen:yuwenfeedback:edit")
    @Log(title = "朗读举报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AudioFeedback audioFeedback) {
        return toAjax(audioFeedbackService.approve(audioFeedback.getId(), audioFeedback.getApproveResult()));
    }

    /**
     * 审核朗读举报
     */
    @RequiresPermissions("yuwen:yuwenfeedback:edit")
    @Log(title = "朗读举报", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approveSave(Long id, Integer approveResult) {
        return toAjax(audioFeedbackService.approve(id, approveResult));
    }

    @RequiresPermissions("yuwen:yuwenfeedback:edit")
    @Log(title = "朗读举报", businessType = BusinessType.UPDATE)
    @PostMapping("/approveReporter")
    @ResponseBody
    public AjaxResult approveReporter(Long id) {
        AudioFeedback audioFeedback = audioFeedbackService.selectAudioFeedbackById(id);
        AudioFeedback query = new AudioFeedback();
        query.setReporter(audioFeedback.getReporter());
        query.setApproveStatus(0);
        List<AudioFeedback> audioFeedbacks = audioFeedbackService.selectAudioFeedbackList(query);
        int count = 0;
        for (AudioFeedback feedback : audioFeedbacks) {
            audioFeedbackService.approve(feedback.getId(), 1);
            count += 1;
        }
        return toAjax(count);
    }

    /**
     * 删除朗读举报
     */
    @RequiresPermissions("yuwen:yuwenfeedback:remove")
    @Log(title = "朗读举报", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(audioFeedbackService.deleteAudioFeedbackByIds(ids));
    }
}
